package com.example.tiantian.contact;

import java.io.ByteArrayOutputStream;

import com.example.tiantian.BaseActivity;
import com.example.tiantian.R;

import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

public class NewContactInfoActivity extends BaseActivity implements OnClickListener {

    public static final String BACK_RESULT = "contact";

    public static final int RESULT_ADD = 2;
    public static final int RESULT_UPDATE = 1;
    public static final int RESULT_NOTHING = 0;

    public static final int REQUEST_GALLERY = 100;

    ImageView imgIcon;
    EditText txtName;
    EditText txtMobile;
    EditText txtOffice;
    EditText txtfamily;
    EditText txtPosition;
    EditText txtCompany;
    EditText txtAddress;
    EditText txtZipcode;
    EditText txtEmail;
    EditText txtOthercontact;
    EditText txtRemark;
    Button btn1;
    Button btnBack;

    ContactHelper helper;

    int state;

    ContactInfo info;

    ViewPager view;

    @Override
    protected void init() {
        setContentView(R.layout.activity_contact);

        imgIcon = (ImageView) this.findViewById(R.id.contact_info_icon);
        txtName = (EditText) this.findViewById(R.id.contact_info_name);
        txtMobile = (EditText) this.findViewById(R.id.contact_info_phone_moblie);
        txtOffice = (EditText) this.findViewById(R.id.contact_info_phone_office);
        txtfamily = (EditText) this.findViewById(R.id.contact_info_phone_family);
        txtCompany = (EditText) this.findViewById(R.id.contact_info_company);
        txtPosition = (EditText) this.findViewById(R.id.contact_info_position);
        txtAddress = (EditText) this.findViewById(R.id.contact_info_address);
        txtEmail = (EditText) this.findViewById(R.id.contact_info_email);
        txtZipcode = (EditText) this.findViewById(R.id.contact_info_zipcode);
        txtOthercontact = (EditText) this.findViewById(R.id.contact_info_other);
        txtRemark = (EditText) this.findViewById(R.id.contact_info_remark);
        btn1 = (Button) this.findViewById(R.id.contact_info_btn);
        btnBack = (Button) this.findViewById(R.id.contact_info_back);
        // imgIcon.setImageResource(R.drawable.image1);// 设置头像的默认图标
        helper = ContactHelper.getHelper(this);
        state = getIntent().getIntExtra(ContactActivity.REQUEST, ContactActivity.REQUEST_CODE_ADD);
        switch (state) {
        case ContactActivity.REQUEST_CODE_EDIT:
            btn1.setText(R.string.save);
            break;
        case ContactActivity.REQUEST_CODE_SHOW:
            btn1.setText(R.string.edit);
            lock();
        default:
            break;
        }
    }

    @Override
    protected void loadData() {
        if (state != ContactActivity.REQUEST_CODE_ADD) {
            info = getIntent().getParcelableExtra(ContactActivity.CONTACTINFO);
            if (info.photo != null) {
                imgIcon.setImageBitmap(BitmapFactory.decodeByteArray(info.photo, 0, info.length));
                Log.d("Test", "photoLoad");
            } else {
                imgIcon.setImageResource(R.drawable.image1);
                Log.d("Test", "photoDefault");
            }

            txtName.setText(info.name);
            txtMobile.setText(info.mobilephone);
            txtOffice.setText(info.officephone);
            txtfamily.setText(info.familyphone);
            txtCompany.setText(info.company);
            txtPosition.setText(info.position);
            txtAddress.setText(info.address);
            txtEmail.setText(info.email);
            txtZipcode.setText(info.zipcode);
            txtOthercontact.setText(info.othercontact);
            txtRemark.setText(info.remark);
        } else {
            info = new ContactInfo();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            back();
        }
        return false;
    }

    @Override
    protected void event() {
        btn1.setOnClickListener(this);
        btnBack.setOnClickListener(this);
        imgIcon.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.contact_info_btn:
            // 返回 同时返回一个联系人对象 到主界面
            Intent intent_back = new Intent(this, NewContactActivity.class);
            switch (state) {
            case ContactActivity.REQUEST_CODE_ADD:
                long id = helper.add(getContact());
                info._id = id;
                // 将联系人对象 绑定上 intent
                setResult(RESULT_ADD, intent_back.putExtra(BACK_RESULT, info));
                toast(info.getName() + getString(R.string.contact_info_added));
                finish();
                break;
            case ContactActivity.REQUEST_CODE_EDIT:
                helper.update(getContact());
                // 将联系人对象 绑定上 intent
                setResult(RESULT_UPDATE, intent_back.putExtra(BACK_RESULT, info));
                toast(info.getName() + getString(R.string.contact_info_updated));
                finish();
                break;
            case ContactActivity.REQUEST_CODE_SHOW:
                state = ContactActivity.REQUEST_CODE_EDIT;
                btn1.setText(R.string.save);
                unlock();
                break;
            default:
                break;
            }
            break;
        case R.id.contact_info_back:
            back();
            break;
        case R.id.contact_info_icon:
            startActivityForResult(new Intent(Intent.ACTION_PICK).setType("image/*"), REQUEST_GALLERY);
            break;
        default:
            break;
        }
    }

    public void back() {
        switch (state) {
        case ContactActivity.REQUEST_CODE_ADD:
            toast(getString(R.string.contact_info_add_cancel));
            break;
        case ContactActivity.REQUEST_CODE_EDIT:
            toast(getString(R.string.contact_info_update_cancel));
            break;
        default:
            break;
        }
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == BaseActivity.RESULT_OK && requestCode == REQUEST_GALLERY) {
            Uri imageuri = data.getData();

            ContentResolver resolver = this.getContentResolver();

            Cursor c = resolver.query(imageuri, null, null, null, null);
            String path = "";
            if (c.moveToNext()) {
                path = c.getString(c.getColumnIndex("_data"));
            }
            Bitmap bitmap = BitmapFactory.decodeFile(path);
            bitmap = Bitmap.createScaledBitmap(bitmap, 200, 200, true);
            imgIcon.setImageBitmap(bitmap);

            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
            info.photo = bos.toByteArray();
            info.length = info.photo.length;
        }
    }

    private void lock() {
        imgIcon.setEnabled(false);

        txtName.setEnabled(false);
        txtMobile.setEnabled(false);
        txtOffice.setEnabled(false);
        txtfamily.setEnabled(false);
        txtCompany.setEnabled(false);
        txtPosition.setEnabled(false);
        txtAddress.setEnabled(false);
        txtEmail.setEnabled(false);
        txtZipcode.setEnabled(false);
        txtOthercontact.setEnabled(false);
        txtRemark.setEnabled(false);
    }

    private void unlock() {
        imgIcon.setEnabled(true);

        txtName.setEnabled(true);
        txtMobile.setEnabled(true);
        txtOffice.setEnabled(true);
        txtfamily.setEnabled(true);
        txtCompany.setEnabled(true);
        txtPosition.setEnabled(true);
        txtAddress.setEnabled(true);
        txtEmail.setEnabled(true);
        txtZipcode.setEnabled(true);
        txtOthercontact.setEnabled(true);
        txtRemark.setEnabled(true);
    }

    private ContactInfo getContact() {

        info.name = txtName.getText().toString();
        info.mobilephone = txtMobile.getText().toString();
        info.familyphone = txtfamily.getText().toString();
        info.officephone = txtOffice.getText().toString();
        info.address = txtAddress.getText().toString();
        info.company = txtCompany.getText().toString();
        info.position = txtPosition.getText().toString();
        info.zipcode = txtZipcode.getText().toString();
        info.email = txtEmail.getText().toString();
        info.othercontact = txtOthercontact.getText().toString();
        info.remark = txtRemark.getText().toString();

        return info;
    }

}
